<template>
  <div>
    <div class="wrapper">
      <div class="line">
        <FormPanel ref="form" :list="formList" :cols="3" :formType="type" :isSubmitBtn="false" />
        <BreakSpace label="项目" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
        <FilterTable
          ref="table1"
          columnsRef="jzyContactsPanel1"
          height="200"
          :columns="columnsXm"
          :fetchapi="fetchApi"
          :params="fetchParams"
          :datakey="'rows'"
          selectionType="single"
          :onColumnsChange="columns => (this.columnsXm = columnsXm)"
        ></FilterTable>
        <BreakSpace label="备件信息" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
        <FilterTable
          ref="table2"
          columnsRef="jzyContactsPanel2"
          height="200"
          :columns="columnsBj"
          :fetchapi="fetchApi2"
          :params="fetchParams2"
          :datakey="'rows'"
          :onColumnsChange="columns => (this.columnsBj = columnsBj)"
        ></FilterTable>
        <BreakSpace label="建议项目" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
        <FilterTable
          ref="table3"
          columnsRef="jzyContactsPanel3"
          height="200"
          :columns="columnsJy"
          :fetchapi="fetchApi3"
          :params="fetchParams3"
          :datakey="'rows'"
          :onColumnsChange="columns => (this.columnsJy = columnsJy)"
        ></FilterTable>
        <BreakSpace label="前台领料" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
        <FilterTable
          ref="table4"
          columnsRef="jzyContactsPanel4"
          height="200"
          :columns="columnsQt"
          :fetchapi="fetchApi4"
          :params="fetchParams4"
          :datakey="'rows'"
          :onColumnsChange="columns => (this.columnsQt = columnsQt)"
        ></FilterTable>

        <div
          :style="{
            position: 'absolute',
            left: 0,
            bottom: 0,
            right: 0,
            zIndex: 9,
            borderTop: '1px solid #e9e9e9',
            padding: '10px 20px',
            background: '#fff',
            textAlign: 'right'
          }"
        >
          <el-button @click="closeHandler">关闭</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// @author:@刘诗文
// selectionType="single"
// :onRowSelectChange="rowSelectHandle"
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { notifyAction, confirmAction } from '@/utils';
import { phone, telePhone } from '@/utils/validate';
import { getXM, getPJ, getJYXM, getQueryAllMaterial } from '@/api/repair/queryStatistics/queryAccounts';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode, searchRoleByCodes } from '@/api/common/empSelect/index';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';

export default {
  name: 'contactsPanel',
  components: {},
  mixins: [dictionary, authority],
  props: ['formData'],

  data() {
    this.selectedRow = null;
    return {
      formList: this.createFormList(),
      columnsXm: this.createTableColumnsXm(),
      columnsBj: this.createTableColumnsBj(),
      columnsJy: this.createTableColumnsJy(),
      columnsQt: this.createTableColumnsQt()
    };
  },
  mounted() {
    this.initSelect();
    this.getAllUser();
  },
  created() {
    this.init();
  },
  methods: {
    async initSelect() {
      let testDrive = { role: dictCode.TECHNICIAN };
      let res = (await searchRoleByCodes(testDrive)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.columns.find(x => x.dataIndex === 'TECHNICIAN').editItems = res;

      let materialAvailable = { role: dictCode.MATERIAL_AVAILABLE };
      let res1 = (await searchRoleByCode(materialAvailable)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.columns.find(x => x.dataIndex === 'RECEIVER').editItems = res1;
    },
    // 项目
    createTableColumnsXm() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目名称',
          dataIndex: 'LABOUR_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // 表tt_wr_claim_orderh 备注
        {
          title: '项目附加说明',
          dataIndex: 'XMFJSM',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目属性',
          dataIndex: 'XMSX',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '服务包代码',
          dataIndex: 'SERVICE_PKG_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修属性',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '性质',
          dataIndex: 'XZ',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时优惠率',
          dataIndex: 'DISCOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '班组名称',
          dataIndex: 'BZ',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主修人',
          dataIndex: 'TECHNICIAN',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editable: false,
          editItems: []
        },
        {
          title: '开始时间',
          dataIndex: 'ITEM_START_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '派工时间',
          dataIndex: 'ASSIGN_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '完工时间',
          dataIndex: 'ITEM_END_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '有效时间',
          dataIndex: 'YXSJ',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '追加项目',
          dataIndex: 'ADD_TAG',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返工',
          dataIndex: 'FG',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '跨期',
          dataIndex: 'KQ',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 备件信息
    createTableColumnsBj() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件别名',
          dataIndex: 'PART_NAME1',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务包代码',
          dataIndex: 'PACKAGE_ITEM_ID',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '出库类型',
          dataIndex: 'IN_OUT_TYPE',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.IN_OUT_TYPE, '8249')}</span>;
          }
        },
        {
          title: '优惠率',
          dataIndex: 'PRICE_RATE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '领料人',
          dataIndex: 'RECEIVER',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editable: false,
          editItems: []
        },
        {
          title: '出库单价',
          dataIndex: 'CKDJ',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出库数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '已退数量',
          dataIndex: 'nReturnQty',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '数量',
          dataIndex: 'TKSL',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '合计金额',
          dataIndex: 'OUT_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '入出库单编号',
          dataIndex: 'SHEET_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '领料方式',
          dataIndex: 'PICK_TYPE',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.PICK_TYPE, '8111')}</span>;
          }
        }
      ];
    },
    // 建议项目
    createTableColumnsJy() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '建议委托书',
          dataIndex: 'ESTIMATE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目名称',
          dataIndex: 'LABOUR_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时费',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '工时',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '转入维修',
          dataIndex: 'ZRWX',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '转入维修委托书',
          dataIndex: 'ZRWXWTS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作员代码',
          dataIndex: 'CREATED_BY',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createTableColumnsQt() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '前台领料领料方式',
          dataIndex: 'pickType',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件代码',
          dataIndex: 'PART_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件别名',
          dataIndex: 'BJBM',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数量',
          dataIndex: 'RECEIVER',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '单价',
          dataIndex: 'BJDJ',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '金额',
          dataIndex: 'SERIES',
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    createFormList() {
      return [];
    },
    rowSelectHandle(rows) {
      if (!rows.length) {
        this.selectedRow = null;
      } else {
        this.selectedRow = rows[0];
      }
    },
    async removeRecordHandle() {
      try {
        await confirmAction('此操作将删除该记录, 是否继续?');
        const res = await removeContactRecord(this.selectedRow.id);
        if (res.resultCode === 200) {
          if (!res.errMsg) {
            notifyAction('删除数据成功！');
            // 执行刷新
            this.refreshHandle();
          } else {
            notifyAction(res.errMsg, 'warning');
          }
        }
      } catch (e) {}
    },
    // 所有用户
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.columnsBj.find(x => x.dataIndex === 'RECEIVER').filterItems = this.columnsBj.find(item => item.dataIndex === 'RECEIVER').editItems = data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
      this.columnsXm.find(x => x.dataIndex === 'TECHNICIAN').filterItems = this.columnsXm.find(item => item.dataIndex === 'TECHNICIAN').editItems = data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
    },
    // async saveHandler() {
    //   try {
    //     // 获取表单控件的值，会执行表单校验，遵循错误前置的原则
    //     const [err, data] = await this.$refs.form.GET_FORM_DATA();
    //     if (err) return;
    //     const res = await saveContactInfo({ vCardId: this.formData.vCardId, vCardNo: this.formData.vCardNo, vCusName: this.formData.vCusName, ...data });
    //     if (res.resultCode === 200) {
    //       notifyAction('联系人保存成功！');
    //       // 执行刷新
    //       this.closeHandler('refresh');
    //     }
    //   } catch (e) {}
    // },
    // 主页面刷新
    refreshHandle() {
      this.fetchParams = { ...this.fetchParams };
    },
    init() {
      this.getParams();
    },
    async getParams() {
      let roNo = this.formData[0].RO_NO;
      console.log(roNo, '121212121');
      let res = await getXM({ RO_NO: roNo });
      console.log(res, '项目');
      this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(res.data.rows);
      let res2 = await getPJ({ RO_NO: roNo });
      console.log(res2, '备件');
      this.$refs.table2.$refs.pageTable.EXECUTE_INSERT(res2.data.rows);
      let res3 = await getJYXM({ RO_NO: roNo });
      console.log(res3);
      this.$refs.table3.$refs.pageTable.EXECUTE_INSERT(res3.data.rows);
      let res4 = await getQueryAllMaterial({ RO_NO: roNo });
      this.$refs.table4.$refs.pageTable.EXECUTE_INSERT(res4.data.rows);
    },
    // 关闭面板
    closeHandler(type) {
      this.$emit('close', false, type);
    }
  }
};
</script>

<style lang="less" scoped></style>
